home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3209 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.4 KB  |  135 lines

  1. Path: news.jhu.edu!news
  2. From: Stephanos Androutsellis-Theotokis <stheotok@bme.jhu.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Question on constructors
  5. Date: Mon, 22 Jan 1996 14:32:18 -0500
  6. Organization: Johns Hopkins University
  7. Message-ID: <3103E642.41C6@bme.jhu.edu>
  8. NNTP-Posting-Host: 128.220.14.26
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b6a (X11; I; IRIX 5.2 IP22)
  13.  
  14. I have the following question on constructors:
  15.  
  16. I wrote a c++ library that uses a dummy non-local static instance of a class 
  17. with a default constructor to force library initialization. 
  18. I thought C++ semantics guaranteed that the constructor for this dummy
  19. object is called before any other functions in the same "translation unit",
  20. or, at the very least, before any member functions of this dummy object 
  21. are called.
  22. Is this assumption wrong?
  23. If so, how can one force a certain constructor to be called before any
  24. others?
  25. If not, why does the following program violate this assumption under my
  26. Watcom v10.0 compiler?
  27.  
  28. File z.cpp:
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #include "mylib.h"
  31. #include <iostream.h>
  32. #include <string.h>
  33.  
  34. class A {
  35.    public:
  36.         A();
  37.         A(char *as);
  38.         ~A();
  39.    private:
  40.         char *s;
  41. };
  42.  
  43. A aa("main global non static");
  44. static A a("main global static");
  45.  
  46. A::A(char *as)
  47. {
  48.  
  49.         cout << "A constructor called for " << as << "\n";
  50.         s = strdup(mylibf1(as));
  51.         cout << "S is now" << s << "\n";
  52. }
  53.  
  54. A::~A()
  55. {
  56.         cout << "A destructor called\n";
  57. }
  58.  
  59. main() {
  60.         A c("main local non static");
  61.         static A d("main local static");
  62. }
  63. ////////////////////////////////////////////////////////////////////////////////
  64. File mylib.h
  65.  
  66. char *
  67. mylibf1(char *as);
  68.  
  69. ////////////////////////////////////////////////////////////////////////////////
  70. File mylib.cpp
  71.  
  72. #include "mylib.h"
  73. #include <string.h>
  74. #include <iostream.h>
  75. #include <malloc.h>
  76.  
  77. class B {
  78.    public:
  79.         B();
  80.         ~B();
  81.         char *libf1(char *as);
  82.    private:
  83.         char *s;
  84. };
  85.  
  86. static B dummy;
  87.  
  88. B::B()
  89. {
  90.    cout << "Constructor for dummy called.\n";
  91.    s = strdup("Initial str");
  92. }
  93.  
  94. B::~B()
  95. {
  96.    cout << "Destructor for dummy called.\n";
  97.    free(s);
  98. }
  99.  
  100. char *
  101. mylibf1(char *as)
  102. {
  103.    return ::dummy.libf1(as);
  104. }
  105.  
  106. char *
  107. B::libf1(char *as)
  108. {
  109.    char *tmp;
  110.  
  111.    tmp = s;
  112.    s = strdup(as);
  113.    return tmp;
  114. }
  115. ////////////////////////////////////////////////////////////////////////////////
  116.  
  117. The program crashes because the constructor for dummy is called after the
  118. constructors for class A objects in file z.cpp,although these objects'
  119. constructors call (indirectly) member functions of "dummy".
  120. Note that if the programs are linked the other way round (e.g. changing
  121. z.cpp to a.cpp) causes the constructors to be executed in the right
  122. order.
  123. The question of how to cause a constructor to be called before anything
  124. else (in any translation unit) also interests me because constructor calling 
  125. sequence determines the sequence in which destructors will be called. In the 
  126. above example, I would like the constructor for "dummy" to be called first 
  127. regardless of whether the constructor for class A objects uses "dummy" members 
  128. or not.  The reason for this is that the destructor for A might use "dummy"
  129. members, hence requiring "dummy" destructor to be called last.
  130.  
  131. Any help would be appreciated.
  132.  
  133. Stephanos.
  134. stheotok@bme.jhu.edu
  135.